package com.shiwaixiangcun.customer.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.adapter.ImageDetailAdapter;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.entity.DecorateDetailBean;
import com.shiwaixiangcun.customer.interfaces.RequestPermissionType;
import com.shiwaixiangcun.customer.ui.dialog.DialogLoginOut;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;

import java.util.List;

import static butterknife.internal.Utils.arrayOf;

/**
 * @author Administrator
 */
public class CompanyDetailToDetailActivity extends BaseActivity implements View.OnClickListener {

    private ChangeLightImageView back_left;
    private List<DecorateDetailBean.DataBean.DecoratePlanDtosBean> serlist;
    private ListView lv_image_detail;
    private int id;
    private Button btn_phone;
    private String phone;
    private TextView tv_title_compay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_detail_to_detail);
        //        百度统计
        StatService.setLogSenderDelayed(10);
        StatService.setSendLogStrategy(this, SendStrategyEnum.APP_START, 1, false);
        StatService.setSessionTimeOut(30);

        Intent intent = getIntent();
        String idDetail = intent.getStringExtra("idDetail");
        id = Integer.parseInt(idDetail);
        phone = intent.getStringExtra("phone");
        serlist = (List<DecorateDetailBean.DataBean.DecoratePlanDtosBean>) intent.getSerializableExtra("serlist");
        Log.i("tttttrrr",serlist.get(0).getPlanName());
        layoutView();
        initData();
    }

    private void layoutView() {
        back_left = findViewById(R.id.back_left);
        lv_image_detail = findViewById(R.id.lv_image_detail);


        View image_head_view = LayoutInflater.from(this).inflate(R.layout.image_head_detial, null);
        tv_title_compay = image_head_view.findViewById(R.id.tv_title_compay);
        lv_image_detail.addHeaderView(image_head_view);

        View image_footer_view = LayoutInflater.from(this).inflate(R.layout.iamge_footer_detail, null);
        btn_phone = image_footer_view.findViewById(R.id.btn_phone);
        lv_image_detail.addFooterView(image_footer_view);
    }

    private void initData() {
        List<DecorateDetailBean.DataBean.DecoratePlanDtosBean.DesignChartsBean> designCharts = serlist.get(id).getDesignCharts();
        if (serlist != null && serlist.size() != 0){
            ImageDetailAdapter imageDetailAdapter = new ImageDetailAdapter(designCharts,this);
            lv_image_detail.setAdapter(imageDetailAdapter);
        }
        String planName = serlist.get(id).getPlanName();

        tv_title_compay.setText(planName);

        back_left.setOnClickListener(this);
        btn_phone.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.back_left:
                finish();
                break;
            case R.id.btn_phone:
                showLoginoutDialog();
                break;
            default:
                break;
        }
    }


    private void showLoginoutDialog() {
        final DialogLoginOut dialogLoginOut = new DialogLoginOut(CompanyDetailToDetailActivity.this, R.layout.item_dialog_call_phone);
        dialogLoginOut.setTitle("是否要拨打此电话？");
        dialogLoginOut.setMessage(phone);
//        dialogLoginOut.setColor();
        dialogLoginOut.setYesOnclickListener("是", new DialogLoginOut.OnPositiveOnclickListener() {
            @Override
            public void onYesClick() {
                requestPermission(phone);
            }
        });

        dialogLoginOut.setNoOnclickListener("否", new DialogLoginOut.OnPassiveOnclickListener() {
            @Override
            public void onNoClick() {

                dialogLoginOut.dismiss();
            }
        });
        dialogLoginOut.show();
    }

    private void requestPermission(String number) {
        //判断Android版本是否大于23
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CALL_PHONE),
                        RequestPermissionType.REQUEST_CODE_ASK_CALL_PHONE);
                return;
            } else {
                callPhone(number);
            }
        } else {
            callPhone(number);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case RequestPermissionType.REQUEST_CODE_ASK_CALL_PHONE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callPhone(phone);
                } else {
                    // Permission Denied
                    showToastShort("未获取拨打电话权限");
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


    /**
     * 拨号方法
     */
    @SuppressLint("MissingPermission")
    private void callPhone(String number) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }
}
